package org.jeecg.modules.third.jg.jss.report.cfcsourcegoodsinfo.service;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.annotation.CurDataSource;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.config.DataSourceNames;
import org.jeecg.modules.third.jg.jss.report.cfccontractinfo.entity.CfcContractinfo;
import org.jeecg.modules.third.jg.jss.report.cfccontractinfo.service.CfcContractinfoService;
import org.jeecg.modules.third.jg.jss.report.cfcshipperinfo.entity.CfcShipperinfo;
import org.jeecg.modules.third.jg.jss.report.cfcshipperinfo.service.CfcShipperinfoService;
import org.jeecg.modules.third.jg.jss.report.cfcsourcegoodsinfo.entity.CfcSourcegoodsinfo;
import org.jeecg.modules.third.jg.jss.report.cfcsourcegoodsinfo.mapper.CfcSourcegoodsinfoMapper;
import org.jeecg.modules.wlhy.company.entity.HyCompany;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerCompany;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.order.entity.HyOrder;
import org.jeecg.modules.wlhy.order.entity.HyOrderDTO;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 货源信息
 * @Description: CFC_SOURCEGOODSINFO
 * @Author: jeecg-boot
 * @Date:   2021-11-25
 * @Version: V1.0
 */
@Service
@Slf4j
public class CfcSourcegoodsinfoService {

    @Resource
    private CfcSourcegoodsinfoMapper sourcegoodsinfoMapper;

    @Resource
    private CfcContractinfoService contractinfoService;

    @Autowired
    private CfcShipperinfoService shipperinfoService;

    private String CONTRACTADD = "temp/20211217/PYDG202136040000003709_1639712160649.pdf";

    public String getEnterpriseCode() {
        return enterpriseCode;
    }

    public void setEnterpriseCode(String enterpriseCode) {
        this.enterpriseCode = enterpriseCode;
    }

    private String enterpriseCode;


    private static final Logger LOGGER = LoggerFactory.getLogger(CfcSourcegoodsinfoService.class);


    @Transactional
    @CurDataSource(name = DataSourceNames.SECOND)
    public boolean insertSourcegoodsinfo(HyOrderDTO hyOrder , HyCustomerCompany customerCompany,
                                         HyCompany company, BigDecimal costPayShipping) {

        CfcSourcegoodsinfo  cfcSourcegoodsinfo = sourcegoodsinfoMapper.selectSourcegoodsinfo(hyOrder.getId());
        if(cfcSourcegoodsinfo != null){
            return false;
        }
        //托运人信息
        CfcShipperinfo  shipperinfo =  shipperinfoService.getShipperinfo(hyOrder.getCustomerId());
        if(shipperinfo == null){
            LOGGER.info("CfcSourcegoodsinfoService  insertSourcegoodsinfo shipperinfo:{}", hyOrder);
            return false;
        }

        if(customerCompany == null){
            LOGGER.info("CfcSourcegoodsinfoService  insertSourcegoodsinfo customerCompany:{}", hyOrder);
            return false;
        }

        if (company == null) {
            LOGGER.info("CfcSourcegoodsinfoService  insertSourcegoodsinfo company:{}", hyOrder);
            return false;
        }

        //合同信息
        CfcContractinfo contractinfoDb = contractinfoService.getContractinfoByNumber(customerCompany.getContractNumber());
        if(contractinfoDb == null){

            CfcContractinfo contractinfo = new CfcContractinfo ();
            //HyConfig systemConfig = HyConfigUtils.getConfig();
            contractinfo.setPlatformcode(this.getEnterpriseCode());

            contractinfo.setContractnumber(customerCompany.getContractNumber());
            contractinfo.setContracttype("1");
            contractinfo.setFirstparty( shipperinfo.getShippername());
            contractinfo.setSecondparty( company.getCompanyName());

            contractinfo.setContractamount(costPayShipping);

            contractinfo.setContractphotoaccessaddress(
                    contractinfoService.getContractAdd(customerCompany.getImgContract())
            );

            contractinfo.setContractdatetime(customerCompany.getCreateTime());

            contractinfoService.insertContractinfo(contractinfo);
        }

        cfcSourcegoodsinfo = hyOrderTOSourcegoodsinfo(hyOrder ,shipperinfo);
        cfcSourcegoodsinfo.setContractnumber(customerCompany.getContractNumber());
        Integer res = sourcegoodsinfoMapper.insertSourcegoodsinfo(cfcSourcegoodsinfo);
        return res > 0 ;
    }


    public CfcSourcegoodsinfo hyOrderTOSourcegoodsinfo(HyOrderDTO hyOrder,CfcShipperinfo shipperinfo){
        if ( hyOrder == null || shipperinfo == null) {
            return null;
        }
        CfcSourcegoodsinfo cfcSourcegoodsinfo = new CfcSourcegoodsinfo();
        cfcSourcegoodsinfo.setPlatformcode(this.getEnterpriseCode());
        cfcSourcegoodsinfo.setSourcegoodsid(hyOrder.getId());

        cfcSourcegoodsinfo.setShipperid(shipperinfo.getShipperid());
        cfcSourcegoodsinfo.setShippername(shipperinfo.getShippername());
        cfcSourcegoodsinfo.setPersonalidentitydocument(shipperinfo.getUnifiedsocialcreditldentifier());

        cfcSourcegoodsinfo.setContactname(hyOrder.getSendContactName());
        cfcSourcegoodsinfo.setTelephonenumber(hyOrder.getSendContactTel());

        cfcSourcegoodsinfo.setCountrysubdivisioncode(hyOrder.getSendAreaCode());
        cfcSourcegoodsinfo.setCountrysubdivisionname(hyOrder.getSendAddressName());
        cfcSourcegoodsinfo.setPlaceofloading(hyOrder.getSendAddress());

        cfcSourcegoodsinfo.setConsignee(hyOrder.getReceiveContactName());
        cfcSourcegoodsinfo.setConsicontactname(hyOrder.getReceiveContactName());
       // cfcSourcegoodsinfo.setConsigneepertnumber(shipperinfo.getUnifiedsocialcreditldentifier());
        cfcSourcegoodsinfo.setConsitelephonenumber(hyOrder.getReceiveContactTel());
        cfcSourcegoodsinfo.setConsicountrysubdivisioncode(hyOrder.getReceiveAreaCode());
        cfcSourcegoodsinfo.setConsicountrysubdivisionname(hyOrder.getReceiveAddressName());
        cfcSourcegoodsinfo.setGoodsreceiptplace(hyOrder.getReceiveAddress());

        if (StrUtil.isNotEmpty(hyOrder.getInsuranceCompany())) {
            cfcSourcegoodsinfo.setInsurancecompany(hyOrder.getInsuranceCompany());
        } else {
            cfcSourcegoodsinfo.setInsurancecompany("中国人民财产保险股份有限公司");
        }

        if (StrUtil.isNotEmpty(hyOrder.getInsuranceCompanyNumber())) {
            cfcSourcegoodsinfo.setInsurancecompanycode(hyOrder.getInsuranceCompanyNumber());
        } else {
            cfcSourcegoodsinfo.setInsurancecompanycode("PICC");
        }

        if (StrUtil.isNotEmpty(hyOrder.getInsurancePolicy())) {
            cfcSourcegoodsinfo.setInsurancebillcode(hyOrder.getInsurancePolicy());
        } else {
            cfcSourcegoodsinfo.setInsurancebillcode("PYDG202136040000003709");
        }

        //TODO 后续有保险业务添加合同地址
        if (StrUtil.isNotEmpty(hyOrder.getInsuranceUrl())) {
            cfcSourcegoodsinfo.setInsuranceaccessaddress(contractinfoService.getContractAdd(hyOrder.getInsuranceUrl()));
        } else {
            cfcSourcegoodsinfo.setInsuranceaccessaddress(contractinfoService.getContractAdd(CONTRACTADD));
        }

        cfcSourcegoodsinfo.setCargotypeclassificationcode(hyOrder.getCargoTypeClassificationCode());
        cfcSourcegoodsinfo.setDescriptionofgoods(hyOrder.getDescriptionOfGoods());
        cfcSourcegoodsinfo.setMeasurementunitcode("TNE");

        if (null != hyOrder.getTotalGoodsWeight()) {
            cfcSourcegoodsinfo.setItemgrossweight(hyOrder.getTotalGoodsWeight());
            BigDecimal totalmonetaryamount = hyOrder.getTotalGoodsWeight().multiply(hyOrder.getShippingUnitPrice());
            cfcSourcegoodsinfo.setTotalmonetaryamount( totalmonetaryamount);
        } else {
            log.warn("货源{}总重量没有填写", hyOrder.getOrderNum());
            cfcSourcegoodsinfo.setItemgrossweight(BigDecimal.ZERO);
            cfcSourcegoodsinfo.setTotalmonetaryamount(BigDecimal.ZERO);
        }

        return cfcSourcegoodsinfo;
    }



    public CfcSourcegoodsinfo selectSourcegoodsinfo( String sourcegoodsid ){
        return  sourcegoodsinfoMapper.selectSourcegoodsinfo(sourcegoodsid);
    }

    public Integer  deletSourcegoodsinfo( String sourcegoodsid){
       return  sourcegoodsinfoMapper.deletSourcegoodsinfo(sourcegoodsid);
    }

}
